<template>
  <div class="notice">
    <basic-container>
      <avue-crud ref="crud"
                 :option="option"
                 :data="tableData"
                 :page="page"
                 v-model="obj"
                 :table-loading="tableLoading"
                 @on-load="getList">
        <template slot="headline"
                  slot-scope="scope">
          <el-button type="text"
                     @click="openDrawer(scope.row)">
            {{scope.row.headline}}
          </el-button>
        </template>
        <template slot-scope="scope"
                  slot="menu">
          <el-button type="text"
                     @click="goNotice(scope.row)">
            <img class="user-icon"
                 :src="userStory">
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
    <drawer :visible="visible"
            :maxWidth="100"
            :width="'90%'"
            :position="'absolute'"
            @close="visible = false">
      <div class="title">
        <span class="color-span">{{$t('navbar.infoMessage')}}</span>
      </div>
      <div class="content">
        <el-form>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item :label="$t('navbar.receiverId')">
                <el-input v-model="currentMess.toUser"
                          disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="$t('navbar.senderId')">
                <el-input v-model="currentMess.sendBy"
                          disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="$t('navbar.sendTime')">
                <el-input v-model="currentMess.sendTime"
                          disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="$t('navbar.messageType')">
                <el-input v-model="currentMess.messageType"
                          disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="$t('navbar.content')">
                <el-input v-model="currentMess.headline"
                          disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="$t('navbar.isRead')">
                <el-select v-model="currentMess.hasRead"
                           disabled>
                  <el-option :label="$t('yesText')"
                             :value="1"></el-option>
                  <el-option :label="$t('noText')"
                             :value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item :label="$t('navbar.infoMessage')">
                <el-input v-model="currentMess.content"
                          disabled
                          type="textarea"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="bottom">
        <el-button @click="visible = false"
                   type="primary">{{$t('submitText')}}</el-button>
      </div>
    </drawer>
  </div>
</template>

<script>
import { getPage, markRead, getInfo } from '../../api/notice'
import { option } from '../../const/notice'
import userStory from '../../assets/img/userStory.svg'
import drawer from '../../components/Drawer/index'

export default {
  components: { drawer },
  data () {
    return {
      option: option(this),
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      obj: {},
      tableLoading: true,
      userStory,
      visible: false,
      currentMess: {}
    }
  },
  methods: {
    getList () {
      this.tableLoading = true
      const obj = {
        current: this.page.currentPage,
        size: this.page.pageSize
      }
      getPage(obj).then(res => {
        this.tableData = res.data.data.records
        this.tableLoading = false
        this.tableData.some(res => {
          if (!res.hasRead) markRead(res.id)
        })
      })
    },
    goNotice (row) {
      getInfo(row.id).then(res => {
        let data = {}
        if (res.data.data) data = res.data.data
        else return
        switch (data.messageType) {
          case 'workflowTask':
            this.$router.push({
              path: '/processManagement/task/index',
              query: { targetId: row.targetId }
            })
            break
        }
      })
    },
    openDrawer (row) {
      this.visible = true
      this.currentMess = row
    }
  }
}
</script>

<style lang="scss" scoped>
.notice {
  position: relative;
  height: 100%;
}
.user-icon {
  width: 14px;
  height: 14px;
}
.content {
  padding: 20px;
}
.title {
  border-bottom: 1px solid #e7e8eb;
  padding: 20px;
  span {
    font-size: 16px !important;
  }
}
.bottom {
  position: absolute;
  bottom: 0;
  width: 100%;
  display: flex;
  flex-flow: row nowrap;
  align-items: center;
  padding: 10px 0;
  justify-content: center;
  border-top: 1px solid #e7e8eb;
}
/deep/.el-select {
  width: 100%;
}
</style>